export function parseTime(time, cFormat) {
	if(!time || time.length === 0) {
		return null
	}
	const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if(typeof time === 'object') {
		date = time
	} else {
		if(('' + time).indexOf('-') < 0 && ('' + time).length === 10) time = parseInt(time) * 1000;
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		if(key === 'a') return ['日', '一', '二', '三', '四', '五', '六'][value]
		if(result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}

export function formatTime(timeoriginal, option) {
	var finalTime = ''
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var d = new Date(time)
		var nowreal = new Date()
		const diff = (nowreal - d) / 1000

		if(diff < 30) {
			return '刚刚'
		} else if(diff < 3600) { // less 1 hour
			return Math.ceil(diff / 60) + '分钟前'
		} else if(diff < 3600 * 24) {
			return Math.ceil(diff / 3600) + '小时前'
		} else if(diff < 3600 * 24 * 2) {
			return '1天前'
		} else {
			var month = d.getMonth() + 1
			month = month < 10 ? '0' + month : month;
			var day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
			return month + '-' + day
		}
	} else {
		return ''
	}
}

function pluralize(time, label) {
	if(time === 1) {
		return time + label
	}
	return time + label + 's'
}

export function timeAgo(time) {
	const between = Date.now() / 1000 - Number(time)
	if(between < 3600) {
		return pluralize(~~(between / 60), ' minute')
	} else if(between < 86400) {
		return pluralize(~~(between / 3600), ' hour')
	} else {
		return pluralize(~~(between / 86400), ' day')
	}
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
	const si = [{
			value: 1E18,
			symbol: 'E'
		},
		{
			value: 1E15,
			symbol: 'P'
		},
		{
			value: 1E12,
			symbol: 'T'
		},
		{
			value: 1E9,
			symbol: 'G'
		},
		{
			value: 1E6,
			symbol: 'M'
		},
		{
			value: 1E3,
			symbol: 'k'
		}
	]
	for(let i = 0; i < si.length; i++) {
		if(num >= si[i].value) {
			return(num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
		}
	}
	return num.toString()
}

export function toThousandslsFilter(num) {
	return(+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

export function tofixNum(num) {
	var newnum = Number(num);
	if(isNaN(newnum)) {
		return num;
	} else {
		if(newnum == 0) {
			return 0
		} else {
			return newnum.toFixed(2);
		}
	}
}
export function tofixTime(second) {
	var durationEx = Number(second);
	var durationStr = ''
	if(!isNaN(durationEx)) {
		var durationHour = (durationEx / 3600);
		if(durationHour >= 1) {
			durationHour = Math.floor(durationHour);
			durationStr += durationHour + '小时'
		}
		var durationSecond = ((durationEx % 3600) / 60);
		if(durationSecond >= 1) {
			durationSecond = Math.floor(durationSecond)
			durationStr += durationSecond + '分钟'
		} else if(durationHour >= 1) {
			durationStr += '0分钟'
		}
		var durationMin = Math.floor((durationEx % 3600) % 60);
		durationStr += durationMin + '秒';

		return durationStr
	} else {
		return second;
	}
}
export function tofixTimeNew(second) {
	var durationEx = Number(second);
	var durationStr = ''
	if(!isNaN(durationEx)) {
		var durationHour = (durationEx / 3600);
		if(durationHour >= 1) {
			durationHour = Math.floor(durationHour);
			durationHour = durationHour < 10 ? '0' + durationHour : durationHour;
			durationStr += durationHour + ':'
		}
		var durationSecond = ((durationEx % 3600) / 60);
		if(durationSecond >= 1) {
			durationSecond = Math.floor(durationSecond)
			durationSecond = durationSecond < 10 ? '0' + durationSecond : durationSecond;
			durationStr += durationSecond + ':'
		} else if(durationHour >= 1) {
			durationStr += '00:'
		}
		var durationMin = Math.floor((durationEx % 3600) % 60);
		durationMin = durationMin < 10 ? '0' + durationMin : durationMin;
		durationStr += durationMin;

		return durationStr
	} else {
		return second;
	}
}
export function tofixTimeSpent(second) {
	var durationEx = Number(second);
	var durationStr = ''
	if(!isNaN(durationEx)) {
		var durationHour = (durationEx / 3600);
		durationHour = Math.floor(durationHour);
		if(durationHour >= 1) {
			durationHour = durationHour < 10 ? '0' + durationHour : durationHour;
		}else{
			durationHour = '00'
		}
		durationStr += durationHour + ':';
		var durationSecond = ((durationEx % 3600) / 60);
		durationSecond = Math.floor(durationSecond)
		if(durationSecond >= 1) {
			durationSecond = durationSecond < 10 ? '0' + durationSecond : durationSecond;
		} else {
			durationSecond = '00'
		}
		durationStr += durationSecond + ':';
		var durationMin = Math.floor((durationEx % 3600) % 60);
		durationMin = durationMin < 10 ? '0' + durationMin : durationMin;
		durationStr += durationMin;
		return durationStr
	} else {
		return second;
	}
}
export function formatMoney(num) {
	if(num) {
		var number = Number(num);
		return '￥ ' + number.toFixed(2);
	}
}
export function spliceForData(timeoriginal) {
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var format = '{h}:{i}'
		var time_str = parseTime(time, format)
		return time_str
	} else {
		return timeoriginal
	}

}
export function spliceForAllData(timeoriginal) {
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var format = '{m}-{d} {h}:{i}'
		var time_str = parseTime(time, format)
		return time_str
	} else {
		return timeoriginal
	}
}
export function spliceForHalfData(timeoriginal) {
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var format = '{y}-{m}-{d}'
		var time_str = parseTime(time, format)
		return time_str
	} else {
		return timeoriginal
	}
}
export function spliceForTask(timeoriginal) {
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var format = '{y}-{m}-{d} {h}:{i}'
		var time_str = parseTime(time, format)
		return time_str
	} else {
		return timeoriginal
	}
}
export function spliceForYear(timeoriginal) {
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var format = '{y}-{m}'
		var time_str = parseTime(time, format)
		return time_str
	} else {
		return timeoriginal
	}
}
export function spliceForDay(timeoriginal) {
	if(timeoriginal) {
		var time = timeoriginal.replace(/-/g, '/'); // 时间格式转换
		time = time.replace(/T/g, ' '); // 时间格式转换
		var format = '{d}'
		var time_str = parseTime(time, format)
		return time_str
	} else {
		return timeoriginal
	}
}
export function spliceForSecond(timeoriginal) {
	if(timeoriginal) {
		var newnum = Number(timeoriginal);
		if(!isNaN(newnum)) {
			newnum = (newnum * 1000).toFixed(0);
			var oldtime = '';
			var hours = parseInt((newnum % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
			hours = hours < 10 ? ('0' + hours) : hours;
			if(hours != '00') {
				oldtime += hours;
				oldtime += ':';
			}
			var minutes = parseInt((newnum % (1000 * 60 * 60)) / (1000 * 60));
			minutes = minutes < 10 ? ('0' + minutes) : minutes
			oldtime += minutes;
			oldtime += ':'
			var seconds = (newnum % (1000 * 60)) / 1000;
			seconds = seconds.toFixed(0);
			seconds = seconds < 10 ? ('0' + seconds) : seconds;
			oldtime += seconds;
			return oldtime
		} else {
			return timeoriginal
		}
	} else {
		return '00:00'
	}
}

export function formatThousand(num) {
	var newNum = num;
	if(typeof(num) == 'number') {
		if(num >= 1000 && num < 10000) {
			newNum = (num / 1000).toFixed(1);
			newNum = newNum + 'k'
		}
		if(num >= 10000) {
			newNum = (num / 10000).toFixed(1);
			newNum = newNum + '万'
		}
	}
	return newNum
}
export function formatThousandC(num) {
	var newNum = num;
	if(typeof(num) == 'number') {
		if(num >= 1000 && num < 10000) {
			newNum = (num / 1000).toFixed(0);
			newNum = newNum + '千'
		}
		if(num >= 10000) {
			newNum = (num / 10000).toFixed(0);
			newNum = newNum + '万'
		}
	}
	return newNum
}
export function spliceQuestion(question) {
	if(question) {
		return question.split('@').join('___')
	} else {
		return question
	}
}
export function filesize(size) {
	if(size && size != '') {
		var file_size = size / 1024 / 1024;
		file_size = file_size.toFixed(2);
		file_size += 'M'
		return file_size
	} else {
		return size
	}
}
export function getIntroduction(introduction) {
	if(introduction && introduction != '') {
		return JSON.parse(introduction).content
	} else {
		return introduction
	}
}